package com.example.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	public final static String EXTRA_MESSAGE = "com.example.contactlist.MESSAGE";
	private ListView listView;
	private ArrayList<String> cats = new ArrayList<String>();
	private String findTheConts;
	private boolean isSearchText = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new PopulateContactMain().execute();
	
		DBAdapter db = new DBAdapter(MainActivity.this);
		db.open();
		cats = db.getCats();
		db.close();
		
		
		Button cat1 = (Button) findViewById(R.id.btnCat1);
		Button cat2 = (Button) findViewById(R.id.btnCat2);
		Button cat3 = (Button) findViewById(R.id.btnCat3);
		Button cat4 = (Button) findViewById(R.id.btnCat4);
		Button cat5 = (Button) findViewById(R.id.btnCat5);
		
		cat1.setText(cats.get(1));
		cat2.setText(cats.get(2));
		cat3.setText(cats.get(3));
		cat4.setText(cats.get(4));
		cat5.setText(cats.get(5));
		
		Intent intent = getIntent();
		isSearchText = false;
	    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
	      String query = intent.getStringExtra(SearchManager.QUERY);
	      findTheConts = query;
	      isSearchText = true;
	      new PopulateContactMain().execute();
	    }
		
	    listView = (ListView) findViewById(R.id.listMain);
	    
		listView.setOnItemClickListener(new OnItemClickListener()		
		{
			public void onItemClick(AdapterView<?> parent, View view, int position, long id){
				TextView txt = (TextView)view.findViewById(R.id.listviewtext2);
				Intent intent = new Intent(MainActivity.this, ViewContact.class);
				intent.putExtra("userid", txt.getText().toString());
				startActivity(intent);
			}
		});
		
	}	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	/** Called when the user clicks the Send button */
	public void sendMessage(View view) {
	}
	
	public void onSearchRequest(View view){
		onSearchRequested();
	}
	
	public void addNewContact(View view){
		Intent myIntent = new Intent(MainActivity.this, AddNewContact.class);
		MainActivity.this.startActivity(myIntent);
		new PopulateContactMain().execute();
	}
	
	public void onClickCategory(View v){
		Button cat = (Button) v;
		Intent myIntent = new Intent(MainActivity.this, CategoryScreen.class);
		myIntent.putExtra("category", cat.getText().toString());
		startActivity(myIntent);
	}
	
	public void onListItemClick(ListView l, View v, int position, long id){
		//send to the view contact screen of the appropriate name
	}
	
	private class PopulateContactMain extends AsyncTask<String, Object, List<Information>>
	{
		//the main screen java file goes inside of parameter 
		//DBAdapter is my database class
		DBAdapter dbAdapter = new DBAdapter(MainActivity.this);
		
		
		@Override
		protected List<Information> doInBackground(String...objects){
			dbAdapter.open();
			if(isSearchText){
				return dbAdapter.searchContacts(findTheConts); 
			} 
			else
				return dbAdapter.populateContactsListView();
		}
		
		protected void onPostExecute(List<Information> result)
		{			
			listView = (ListView) findViewById(R.id.listMain);
			List<Map<String, String>> contacts = new ArrayList<Map<String, String>>();
			String[] rows = {"name", "id"};
			for(int i = 0; i < result.size(); i++)
			{
				HashMap<String, String> map = new HashMap<String, String>();
				//initialize row
				map.put("name", result.get(i).getName());
				map.put("id", result.get(i).getUserId());
				contacts.add(map);
			}
			
			SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, contacts, R.layout.custome_row_view, rows, new int[] {R.id.listviewtext1, R.id.listviewtext2});
			listView.setAdapter(adapter);

			dbAdapter.close();
		}

	}
}